//
//  FavoritsViewController.m
//  FinApp
//
//  Created by Marcel Arbó Lack on 11/11/11.
//  Copyright (c) 2011 FIB-Fàcultat d'informàtica de Barcelona. All rights reserved.
//

#import "FavoritsViewController.h"
#import "FlatListNearCell.h"
#import "FavoritsController.h"
#import "DetailViewController.h"

@implementation FavoritsViewController

@synthesize navigationBar;
@synthesize flats = _flats;
@synthesize flatsTable = _flatsTable;
@synthesize backgroundView, noResultsLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"_favorits", @"");
        self.tabBarItem.image = [UIImage imageNamed:@"tabbar_icon_favoritos"];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [self setNavigationBar:nil];
    [self setNoResultsLabel:nil];
    [self setBackgroundView:nil];
    [_flatsTable release];
    [_flats release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //Set title text
    [navigationBar.topItem setTitle:NSLocalizedString(@"_favorits", @"")];
    
    [self.navigationController setNavigationBarHidden:YES];
    
    [noResultsLabel setText:NSLocalizedString(@"_noResultsFavoritd", @"")];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [self setNavigationBar:nil];
    [self setNoResultsLabel:nil];
    [self setBackgroundView:nil];
    [_flatsTable release];
    [_flats release];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    _flats = [[NSMutableArray alloc] initWithArray:[FavoritsController retrieveAllFavourites]];
    
    if ([_flats count] > 0) {
        //flats on favourites
        [_flatsTable setHidden:NO];
        [backgroundView setHidden:YES];
        [noResultsLabel setHidden:YES];
    }
    else {
        [_flatsTable setHidden:YES];
        [backgroundView setHidden:NO];
        [noResultsLabel setHidden:NO];
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
	return 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 96;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
	return  [_flats count];
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row%2 == 0)
		[cell setBackgroundColor:[UIColor colorWithRed:62/255.0 green:155/255.0 blue:130/255.0 alpha:0.25]];
	else
		[cell setBackgroundColor:[UIColor colorWithRed:241/255.0 green:241/255.0 blue:241/255.0 alpha:1]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"FlatListNearCell";
    FlatListNearCell *cell = (FlatListNearCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
	if (cell == nil) { 
		UIViewController *customCell = [[UIViewController alloc] initWithNibName:@"FlatListNearCell" bundle:nil];
        cell = (FlatListNearCell*)customCell.view;
        [customCell release];
    }
    
	[cell setCellComponentsWithFlat:[_flats objectAtIndex:indexPath.row]];
    [cell.distanceLabel setHidden:YES];
    
	return cell;
}

#pragma mark Table Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	ListFlat *flat = [_flats objectAtIndex:indexPath.row];
    
    DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    [detailVC setFlat:(Flat*)flat];
    [detailVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}

@end
